/**
 * 猪脚
 */
class Role extends egret.DisplayObjectContainer {
    /** 加载猪脚图片资源 */
    private role_png: egret.Bitmap = Main.createBitmapByName("role_png");

    /**  */
    private roleStartPoint: egret.Point = new egret.Point();
    private roleAssitsPoint = { x: 0, y: 0 };
    private roleEndPoint = { x: 0, y: 0 };

    private blockSize = 82;
    /** 1234 上右下左 初始向下 */
    private fangxiang = 3;
    /** 记录总步数 */
    private totalJump: number = 1;

    /** 初始触摸点 */
    private startPoint = new egret.Point();

    public constructor() {
        super();
        this.once(egret.Event.ADDED_TO_STAGE, this.init, this);
    }

    private init() {
        // /** 绘制角色  */
        this.addChild(this.role_png);
        Main.anchorCenter(this.role_png);
        this.role_png.anchorOffsetY = this.role_png.height;
        this.role_png.touchEnabled = true;
        this.role_png.addEventListener(egret.TouchEvent.TOUCH_BEGIN, this.touchStart, this);

        this.roleStartPoint.x = this.x = 803;
        this.roleStartPoint.y = this.y = 422;


        this.roleAssitsPoint = { x: this.roleStartPoint.x - 100, y: this.roleStartPoint.y + this.blockSize / 2 };
        this.roleEndPoint = { x: this.roleStartPoint.x, y: this.roleStartPoint.y + this.blockSize };
    }
    /** 转弯计算 */
    private computedNext() {

        this.roleStartPoint.x = this.roleEndPoint.x;
        this.roleStartPoint.y = this.roleEndPoint.y;

        if (this.fangxiang == 1) {
            this.roleAssitsPoint = { x: this.roleStartPoint.x - 100, y: this.roleStartPoint.y + this.blockSize / 2 };
            this.roleEndPoint = { x: this.roleStartPoint.x, y: this.roleStartPoint.y - this.blockSize };
        } else if (this.fangxiang == 2) {
            this.roleAssitsPoint = { x: this.roleStartPoint.x + this.blockSize / 2, y: this.roleStartPoint.y - 100 };
            this.roleEndPoint = { x: this.roleStartPoint.x + this.blockSize, y: this.roleStartPoint.y };
        }
        else if (this.fangxiang == 3) {
            this.roleAssitsPoint = { x: this.roleStartPoint.x - 100, y: this.roleStartPoint.y + this.blockSize / 2 };
            this.roleEndPoint = { x: this.roleStartPoint.x, y: this.roleStartPoint.y + this.blockSize };
        }
        else if (this.fangxiang == 4) {
            this.roleAssitsPoint = { x: this.roleStartPoint.x + this.blockSize / 2, y: this.roleStartPoint.y - 100 };
            this.roleEndPoint = { x: this.roleStartPoint.x - this.blockSize, y: this.roleStartPoint.y };
        }


    }

    private touchStart(evt: egret.TouchEvent) {
        /** 检测是否开启了拖动模式 */
        if (!ConfigParam.dragControle.role) return;
        Main.superStopPropagation(evt);
        this.startPoint.x = evt.stageX;
        this.startPoint.y = evt.stageY;
        this.addEventListener(egret.TouchEvent.TOUCH_MOVE, this.dragRole, this);
        this.addEventListener(egret.TouchEvent.TOUCH_END, this.touchEnd, this);
    }

    private dragRole(evt: egret.TouchEvent) {
        const comput = new egret.Point();
        comput.x = evt.stageX - this.startPoint.x;
        comput.y = evt.stageY - this.startPoint.y;
        this.startPoint.x = evt.stageX;
        this.startPoint.y = evt.stageY;

        this.x += comput.x;
        this.y += comput.y;

        this.conputeCenter();

        // console.log(`x: ${this.x}`, `y: ${this.y}`);
    }

    private touchEnd(evt: egret.TouchEvent) {
        this.removeEventListener(egret.TouchEvent.TOUCH_MOVE, this.dragRole, this);
    }

    private arr = RES.getRes("mapPath_json");
    private eventArr = RES.getRes("eventData_json");
    /** 前进!!! */
    public jump(Path) {
        if (this.totalJump > 144) return this.gameOver();

        this.conputeCenter();

        if (this.totalJump >= this.arr[0].num) {
            this.fangxiang = this.arr[0].fangxiang;
            this.arr.shift();
        }

        if (this.totalJump >= this.eventArr[0].num) {
            this.eventRun(this.eventArr[0].id);
            this.eventArr.shift();
            return;
        }


        if (Path < 1) return;
        egret.Tween.get(this).to({ factor: 1 }, 300).call(() => {
            Path--;
            this.totalJump++;
            this.jump(Path);
            this.computedNext();
        });
    }

    //添加factor的set,get方法,注意用public  
    public get factor(): number {
        return 0;
    }
    /**
     * 计算方法参考 二次贝塞尔公式 
     * 这里有三个点, 起始点, 辅助点, 终点
     * 也就是说每走一步要获得两个点
     */
    public set factor(value: number) {
        const startPoint = this.roleStartPoint;
        const endPoint = this.roleEndPoint;
        const assitsPoint = this.roleAssitsPoint;
        this.x = (1 - value) * (1 - value) * startPoint.x + 2 * value * (1 - value) * assitsPoint.x + value * value * endPoint.x;
        this.y = (1 - value) * (1 - value) * startPoint.y + 2 * value * (1 - value) * assitsPoint.y + value * value * endPoint.y;
    }


    public static roleCenter: egret.Point = new egret.Point();
    /** 计算角色和屏幕中心的距离, 用来拖动屏幕 */
    private conputeCenter() {
        Role.roleCenter.x = this.x;
        Role.roleCenter.y = this.y;
    }

    /** 事件触发: 调起弹窗 */
    private alertData = RES.getRes("alertData_json");
    private totalHeight: number = 0;
    private eventRun(alertID: number) {
        /** 当前数据 */
        const nowPanel = this.alertData.find(obj => obj.id == alertID);

        /** 遮罩 */
        const alertMask = new egret.Shape();
        this.stage.addChild(alertMask);
        alertMask.graphics.beginFill(0, .5);
        alertMask.graphics.drawRect(0, 0, this.stage.stageWidth, this.stage.stageHeight);
        alertMask.graphics.endFill();
        alertMask.touchEnabled = true;
        alertMask.addEventListener(egret.TouchEvent.TOUCH_BEGIN, Main.superStopPropagation, this);

        /** 弹窗容器 */
        const alertPanel = new eui.Panel();
        this.stage.addChild(alertPanel);
        alertPanel.skinName = "resource/alert_panel_skin/PanelSkin.exml";
        Main.anchorCenter(alertPanel);
        alertPanel.x = this.stage.stageWidth * .5;
        alertPanel.y = this.stage.stageHeight * .5;
        alertPanel.scaleX = .5;
        alertPanel.scaleY = .5;
        alertPanel.alpha = .5;
        alertPanel.addEventListener(eui.UIEvent.CLOSING, () => {
            this.stage.removeChild(alertMask);
        }, this);

        /** 弹窗动画 */
        egret.Tween.get(alertPanel)
            .to({ scaleX: 1.1, scaleY: 1.1, alpha: 1 }, 250)
            .to({ scaleX: 1, scaleY: 1 }, 100)

        /** 标题 */
        const title = new eui.Label();
        title.text = nowPanel.title;
        title.size = 45;
        title.bold = true;
        title.lineSpacing = 10;
        title.width = alertPanel.width * .8;
        title.x = alertPanel.width * .1;
        title.textColor = 0x333333;
        title.y = 90;
        title.textAlign = "center";
        alertPanel.addChild(title);
        this.totalHeight = title.height + title.y + 40;

        const image = new eui.Image();
        image.texture = RES.getRes(`alert_${alertID}_1_jpg`);
        image.width = alertPanel.width * .8;
        image.x = alertPanel.width * .1;
        image.y = this.totalHeight;
        alertPanel.addChild(image);
        this.totalHeight = image.height + image.y;

        /** 简介 */
        const discription = new eui.Label();
        discription.text = nowPanel.discription;
        discription.size = 25;
        discription.bold = true;
        discription.lineSpacing = 10;
        discription.textColor = 0x333333;
        discription.width = alertPanel.width * .8;
        discription.x = alertPanel.width * .1;
        discription.y = this.totalHeight + 30;
        alertPanel.addChild(discription);
        this.totalHeight = discription.height + discription.y;

        /** 地址 */
        const _path = new eui.Label();
        _path.text = "地址:" + nowPanel.path;
        _path.size = 25;
        _path.bold = true;
        _path.lineSpacing = 10;
        _path.textColor = 0x333333;
        _path.width = alertPanel.width * .8;
        _path.x = alertPanel.width * .1;
        _path.y = this.totalHeight + 30;
        alertPanel.addChild(_path);

    }

    private gameOver() {
        /**弹窗容器 */
        const container = new egret.Sprite();
        this.stage.addChild(container);
        container.graphics.beginFill(0, .5);
        container.graphics.drawRect(0, 0, this.stage.stageWidth, this.stage.stageHeight);
        container.graphics.endFill();
        container.touchEnabled = true;
        container.addEventListener(egret.TouchEvent.TOUCH_BEGIN, Main.superStopPropagation, this);

        /** 弹窗背景图 */
        const alertImg = Main.createBitmapByName(`end_png`);
        const oH = alertImg.width / alertImg.height;
        container.addChild(alertImg);
        alertImg.width = this.stage.stageWidth * .9;
        alertImg.height = alertImg.width / oH;
        Main.anchorCenter(alertImg);
        alertImg.x = this.stage.stageWidth * .5;
        alertImg.y = this.stage.stageHeight * .5;
        alertImg.touchEnabled = true;
        alertImg.addEventListener(egret.TouchEvent.TOUCH_TAP, ()=> {
            window.location.reload();
        }, this);

    }

}